package com.wangyi.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.wangyi.sdk.OAuth2AccessToken;

public class OAuthUtil {
	public final static String WANGYI_REFRESH_TOKEN = "refresh_token";
	public final static String WANGYI_ACCESS_TOKEN = "access_token";
	public final static String WANGYI_EXPIRES_IN = "expires_in";
	private static OAuthUtil oAuthUtil = null;
	private SharedPreferences preferences = null;

	private OAuthUtil(Context context) {
		preferences = context.getSharedPreferences("wangyiweibo.oauthtoken", Context.MODE_PRIVATE);
	}

	public static OAuthUtil getInstance(Context context) {
		if (oAuthUtil == null) {
			oAuthUtil = new OAuthUtil(context);
		}
		return oAuthUtil;
	}

	public void saveOAuthAccessToken(OAuth2AccessToken accesstoken) {
		if (accesstoken != null) {
			SharedPreferences.Editor editor = preferences.edit();
			editor.putString(OAuthUtil.WANGYI_REFRESH_TOKEN, accesstoken.getRefresh_token());
			editor.putString(OAuthUtil.WANGYI_ACCESS_TOKEN, accesstoken.getAccess_token());
			editor.putLong(OAuthUtil.WANGYI_EXPIRES_IN, accesstoken.getExpires_in());
			editor.commit();
		}
	}

	public OAuth2AccessToken getOAuthAccessToken() {
		OAuth2AccessToken accesstoken = new OAuth2AccessToken();
		accesstoken.setRefresh_token(preferences.getString(OAuthUtil.WANGYI_REFRESH_TOKEN, null));
		accesstoken.setAccess_token(preferences.getString(OAuthUtil.WANGYI_ACCESS_TOKEN, null));
		accesstoken.setExpires_in(preferences.getLong(OAuthUtil.WANGYI_EXPIRES_IN, 0));
		return accesstoken;
	}
}
